<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/10/25

 * Time: 15:59

 */



namespace app\index\controller;

use app\console\model\GiftOrderTwo;

use app\console\model\SystemCard;

use think\Cache;

use think\Db;





class GiftOrder

{





    public function create1()

    {



        $gift_count_list = Db::name('gift_count')->where('gift_id', 25)->column('single_id,gift_count,gift_count gift_origin_count', 'id');

        $arr = ['order_id' => 1];

        return ($gift_count_list);

        foreach ($gift_count_list as $k => &$v) {

            //$v['order_id'] = 1;

            array_push($v, $arr);

            //$v = array_merge($v, $arr);

        }

//        array_walk($gift_count_list, function (&$value, $key, $arr) {

//            $value = array_merge($value, $arr);

//        }, $arr);

        halt($gift_count_list);

        //$ret = Db::name('order_gift_count')->insertAll($gift_count_list);

        //halt($gift_count_list);

    }



    /**

     * @param $id

     * @param $member_id

     * @param int $single_id

     * @param int $bill

     * @param string $bill_type

     * @param int $bill_header

     * @param string $bill_content

     * @param string $bill_email

     * @param string $bill_phone

     * @param string $explain

     * @param int $shipping

     * @param int $pay_way

     * @return array

     * 创建礼包订单

     */

    public function create($member_coupon_id = 0, $device = 1, $order_online = 1, $shop_id = 0, $t_type = 1, $id, $member_id, $single_id = 0, $bill = 1, $bill_type = '', $bill_header = 0, $bill_content = '', $bill_email = '', $bill_phone = '', $explain = '', $shipping = 1, $pay_way = 1, $order_is_free = 1, $car_number = '')

    {



        $car_number = $this->car_number($car_number, $member_id);

        //return ['code' => 400,'message' => date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')+365,date('Y')))];

        if ($t_type == 2) { //形参传递

            return $this->create_member_card($order_online, $shop_id, $t_type, $id, $member_id, $single_id = 0, $bill = 1, $bill_type = '', $bill_header = 0, $bill_content = '', $bill_email = '', $bill_phone = '', $explain = '', $shipping, $pay_way, $order_is_free);

        }

        //sql默认t_type = 1 如果$t_type = 2 就是 就是会员卡

        Db::startTrans();

        try {

            $gift = Db::name('gift')->where('id', $id)->find();

//            if($gift['day'] <= 0){

//                return ['code' => 400, 'message' => '礼包未设置使用期限'];

//            }

            if ($gift['sycs'] <= 0) {

                return ['code' => 400, 'message' => '剩余次数不足'];

            }

            $member = Db::name('member')->find($member_id);

            $order_number = order_number();

            $shop_order_number = shop_order_number($member_id);

            $username = Db::name('member')->where('id', $member_id)->value('username');

            $single_title = Db::name('single')->where('id', $single_id)->value('cat_name');

            $member_coupon = Db::name('member_coupon')->where('id', $member_coupon_id)->find();

            if (is_null($member_coupon)) {

                $member_coupon['title'] = '';

                $member_coupon['content'] = '';

                $member_coupon['minus_price'] = 0;

                $member_coupon['total_price'] = 0;

            }

            $amount = $gift['sale_price'] - $member_coupon['minus_price'];

            if ($amount <= 0) {

                $amount = 0.01;

            }

            $order = [

                'coupon_id' => $member_coupon_id,

                'coupon_title' => $member_coupon['title'],

                'coupon_content' => $member_coupon['content'],

                'coupon_price' => $member_coupon['minus_price'],

                'coupon_totol_price' => $member_coupon['total_price'],

                'car_number' => $car_number,

                'device' => $device,

                'finance_order' => set_finance_order($member_id),

                'order_online' => 1,

                'order_number' => $order_number,

                'shop_order_number' => $shop_order_number,

                'order_type' => 3,

                'service_type_id' => 0,//1保险2年检3 4个没有附表的分类

                'member_id' => $member_id,

                'username' => $username,

                'amount' => $amount,

                'order_price' => $gift['sale_price'],

                'sale_price' => $gift['sale_price'],

                'gift_origin_price' => $gift['origin_price'],

                'pay_way' => $pay_way,//线上

                'shipping' => $shipping,

                'explain' => $explain,

                'bill' => $bill,//是否开发票

                'bill_type' => $bill_type,//是否开发票

                'bill_header' => $bill_header,//是否开发票

                'bill_content' => $bill_content,//是否开发票

                'bill_email' => $bill_email,//是否开发票

                'bill_phone' => $bill_phone,

                'status' => 1,//未支付-待支付

                'gift_id' => $id,

                'gift_title' => $gift['title'],

                'gift_describe' => $gift['describe'],

                'gift_content' => $gift['content'],

                'gift_start_time' => $gift['start_time'],

                'gift_end_time' => $gift['end_time'],

                'gift_total_count' => $gift['count'],

                'per' => $gift['per'],

                'day' => $gift['day'],

                'use_time' => date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d') + $gift['day'], date('Y'))),

                'create_time' => date('Y-m-d H:i:s', time())

            ];

            Db::name('member_coupon')->where('id', $member_coupon_id)->setField('status', 2);

            $order_id = Db::name('order')->insertGetId($order);



            Db::name('gift')->where('id', $id)->setInc('sale_number', 1);//销量

            Db::name('gift')->where('id', $id)->setDec('sycs', 1); //剩余次数



            $gift_count_list = Db::name('gift_count')->where('gift_id', $id)->column('single_content,single_id,gift_count,gift_count gift_origin_count,day');

            $arr = ['order_id' => $order_id, 'start_time' => date('Y-m-d H:i:s')];

            array_walk($gift_count_list, function (&$value, $key, $arr) {

                $value['start_time'] = date('Y-m-d H:i:s', time());

                $value['end_time'] = date('Y-m-d H:i:s', time() + $value['day'] * 3600 * 24);

                $value = array_merge($value, $arr);

            }, $arr);

            $ret = Db::name('order_gift_count')->insertAll($gift_count_list);



            if ($order_id && $ret) {

                Db::commit();

                return ['code' => 200, 'message' => '礼包订单生成成功', 'order_number' => $order_number];

            }



            return ['code' => 400, 'message' => '失败'];



        } catch (\Exception $e) {

            Db::rollback();

            return ['code' => $e->getCode(), 'message' => $e->getMessage(), 'line' => $e->getLine()];

        }

    }



    /**

     * @param $id

     * @param $member_id

     * @param int $single_id

     * @param int $bill

     * @param string $bill_type

     * @param int $bill_header

     * @param string $bill_content

     * @param string $bill_email

     * @param string $bill_phone

     * @param string $explain

     * @param int $shipping

     * @param int $pay_way

     * @return array

     * 创建会员卡订单

     */



    public function car_number($car_number, $member_id)

    {

        if (empty($car_number)) {

            //用于给前台下单使用

            $member_car_number = Db::name('member')->where('id', $member_id)->value('car_number');

            $member_car_list_car_number = Db::name('member_car_list')->where('member_id', $member_id)->column('car_number');

            if (empty($member_car_number)) {

                $car_number = '';

            }

            if (!empty($member_car_number)) {

                if (empty($member_car_list_car_number)) {

                    $car_number = $member_car_number;

                }

            }

            if (!empty($member_car_number) && !empty($member_car_list_car_number)) {

                $car_number = $member_car_number . '-' . implode('-', $member_car_list_car_number);

            }

        }

        return $car_number;

    }

	public function car_model($car_model, $member_id)

    {

        if (empty($car_number)) {

            //用于给前台下单使用

            $member_car_number = Db::name('member')->where('id', $member_id)->value('car_model');

            $member_car_list_car_number = Db::name('member_car_list')->where('member_id', $member_id)->column('car_model');

            /*if (empty($member_car_number)) {

                $car_number = '';

            }*/

            /*if (!empty($member_car_number)) {

                if (empty($member_car_list_car_number)) {

                    $car_model = $member_car_number;

                }

            }*/

            if (!empty($member_car_list_car_number)) {

                $car_model =implode('-', $member_car_list_car_number);

            }

        }
        return $car_model;

    }




    public function create_member_card($order_online = 1, $shop_id = 0, $t_type = 2, $id, $member_id, $single_id = 0, $bill = 1, $bill_type = '', $bill_header = 0, $bill_content = '', $bill_email = '', $bill_phone = '', $explain = '', $shipping = 1, $pay_way = 1, $order_is_free = 1, $car_number = '')

    {

        $car_number = $this->car_number($car_number, $member_id);

        //sql默认t_type = 1 如果$t_type = 2 就是 就是会员卡

        Db::startTrans();

        try {

            $system_card = Db::name('system_card')->where('id', $id)->find();

            $count = 10000;

            if ($system_card['type'] == 2) {

                $count = $system_card['count'];

            }

            if (empty($system_card)) {

                return ['code' => 200, 'message' => '错误'];

            }

//            if($system_card['day'] <= 0){

//                //return ['code' => 400, 'message' => '礼包未设置使用期限'];

//            }

//            if($system_card['sycs'] <=0 ){

//                return ['code' => 400, 'message' => '剩余次数不足'];

//            }

            $member = Db::name('member')->find($member_id);

            $order_number = order_number();

            $shop_order_number = shop_order_number($member_id);

            $username = Db::name('member')->where('id', $member_id)->value('username');

            $single_title = Db::name('single')->where('id', $single_id)->value('cat_name');

            $amount = $system_card['sale_price'];

            if ($order_is_free == 6) {

                //$amount = 0;

            }

            $order = [

                'car_number' => $car_number,

                'finance_order' => set_finance_order($member_id, $shop_id),

                'order_online' => $order_online,

                'order_number' => $order_number,

                'shop_order_number' => $shop_order_number,

                'order_type' => 3,

                'service_type_id' => 0,//1保险2年检3 4个没有附表的分类

                'member_id' => $member_id,

                'username' => $username,

                'amount' => $amount,

//                'sale_price' => $gift['sale_price'],

//                'gift_origin_price' => $gift['origin_price'],

                'pay_way' => $pay_way,//线上

                'shipping' => 2,

                'explain' => $explain,

                'bill' => $bill,//是否开发票

                'bill_type' => $bill_type,//是否开发票

                'bill_header' => $bill_header,//是否开发票

                'bill_content' => $bill_content,//是否开发票

                'bill_email' => $bill_email,//是否开发票

                'bill_phone' => $bill_phone,

                'status' => $order_is_free,//未支付-待支付

//                'gift_id' => $id,

//                'gift_title' => $gift['title'],

//                'gift_describe' => $gift['describe'],

//                'gift_content' => $gift['content'],

//                'gift_start_time' => $gift['start_time'],

//                'gift_end_time' => $gift['end_time'],

//                'gift_total_count' => $gift['count'],

//                'per' => $gift['per'],

//                'day' => $gift['day'],

                't_type' => 2,

                'mc_type' => $system_card['type'],

                'mc_title' => $system_card['title'],

                'mc_count' => $count,

                'mc_single_pic' => $system_card['single_pic'],

                'mc_start_time' => date('Y-m-d H:i:s', time()),

                'mc_end_time' => date('Y-m-d H:i:s', time() + $system_card['day'] * 3600 * 24),

                'mc_day' => $system_card['day'],

                'system_card_id' => $system_card['id'],

                'shop_id' => $shop_id,

//                'use_time' => date('Y-m-d H:i:s',mktime(0,0,0,0,367,0)),

                'create_time' => date('Y-m-d H:i:s', time())

            ];

            $order_id = Db::name('order')->insertGetId($order);



            Db::name('system_card')->where('id', $id)->setInc('sale_number', 1);//销量

            Db::name('system_card')->where('id', $id)->setDec('sycs', 1); //剩余次数

//

//            $gift_count_list = Db::name('gift_count')->where('gift_id',$id)->column('single_id,gift_count,gift_count gift_origin_count');

//            $arr = ['order_id' => $order_id]; //对数组中的每个元素使用自定义函数

//            array_walk($gift_count_list, function (&$value, $key, $arr) {

//                $value = array_merge($value, $arr);

//            }, $arr);

//            $ret = Db::name('order_gift_count')->insertAll($gift_count_list);



            $gift_count_list = Db::name('system_card_count')->where('gift_id', $id)->column('single_content,single_id,gift_count,gift_count gift_origin_count');

            $arr = ['order_id' => $order_id];

            array_walk($gift_count_list, function (&$value, $key, $arr) {

                //$value['start_time'] = date('Y-m-d H:i:s',time());

                //$value['end_time'] = date('Y-m-d H:i:s',time()+$value['day']*3600*24);

                $value = array_merge($value, $arr);

            }, $arr);

            $ret = Db::name('order_mc_count')->insertAll($gift_count_list);

            if ($order_id) {

                if ($order_is_free != 1) { //默认1收费

                    $member_card_array = [

                        'car_number' => $car_number,

                        'shop_id' => $shop_id,

                        'member_id' => $member_id,

                        'username' => $username,

                        'title' => $system_card['title'],

                        'content' => $system_card['content'],

                        'count' => $count,

                        'minus_count' => $count,

                        'start_time' => date('Y-m-d H:i:s', time()),

                        'end_time' => date('Y-m-d H:i:s', time() + $system_card['day'] * 3600 * 24),

                        'type' => $system_card['type'],

                        'system_card_id' => $system_card['id'],

                        'sale_price' => $system_card['sale_price'],

                        'origin_price' => 0,

                        'order_id' => $order_id,

                        'order_number' => $order_number,

                        'single_pic' => $system_card['single_pic'],

                        'create_time' => date('Y-m-d H:i:s', time())

                    ];

                    Db::name('member_card')->insert($member_card_array);



                }

                if ($order_is_free == 1) {

                    $m_title = '您的订单(会员卡)已下单,请支付';

                }

                if ($order_is_free == 6) {

                    $m_title = '您的订单(会员卡)已下单,请查看';

                }

                Db::name('member_message')->insert(

                    ['member_id' => $member_id,

                        'order_number' => $order_number,

                        'type_id' => 2,

                        'title' => $m_title,

                        'content' => '会员卡可到店使用',

                        'is_click' => 1,

                        'create_time' => date('Y-m-d H:i:s', time())

                    ]);

                (new \app\index\controller\MemberMessage())->jpushMessage($member_id, $m_title, '会员卡可到店使用', 2);

                Db::commit();

                return ['code' => 200, 'message' => '礼包订单(会员卡)生成成功', 'order_number' => $order_number, 'order_id' => $order_id];

            }



            return ['code' => 400, 'message' => '失败'];



        } catch (\Exception $e) {

            Db::rollback();

            return ['code' => $e->getCode(), 'message' => $e->getMessage(), 'line' => $e->getLine()];

        }

    }



    /**

     * 系统会员卡列表

     */

    public function system_card_list()

    {

        //['day' => ['gt',0],'type' => 2 ,'count' => ['gt',0]]

        //SELECT `id`,`title`,`single_pic`,`type`,`count`,`day` FROM `sun_system_card` WHERE `day` > 0 AND `type` = 1 OR `day` > 0 OR `type` = 2 OR `count` > 0 LIMIT 0,6

        $where['day'] = ['gt', 0];

        $where['type'] = ['eq', 1];

        $where1['day'] = ['eq', 0];

        $where1['type'] = ['eq', 2];

        $where1['count'] = ['gt', 0];

        $data = (new SystemCard())->field('id,title,single_pic,type,count,day')->where('(day > 0 and type = 1) or ( day > 0 and type = 2 and count > 0)')->paginate(6);

        return ['code' => 200, 'message' => '系统会员卡列表', 'data' => $data];

    }



    /**

     * 二级礼包

     */

    public function gift2_view($id, $m_id)

    {

        $data = (new GiftOrderTwo())->find($id);

        $is_click = Db::name('member_message')->where('id', $m_id)->value('is_click');

        if ($is_click == 1) {

            Db::name('member_message')->where('id', $m_id)->setField('is_click', '2');

        }

        return ['code' => 200, 'message' => '礼包二级订单详情', 'data' => $data];

    }



//礼包二级订单统一

    public function gift2_agree($id)

    {

        $ret = (new GiftOrderTwo())->where('id', $id)->setField('status', 2);

        return ['code' => 200, 'message' => '好友使用礼包二级订单已确认使用'];

    }



    // 礼包提交

    public function tijiao()

    {

        $param = input();

        if(empty($param['assignor_id'])){

            return ['code' =>400,'message' => '请添加人员'];

        }

        for ($i = 0; $i < count($param['assignor_id']); $i++) {





            $shop_id = \think\Request::instance()->param()['shop_id'][$i];



            $shop_assignor_records[$i]['shop_id'] = Db::name('shop')->where('id',$shop_id)->value('shop_title');

            $shop_assignor_records[$i]['assignor_id'] = Db::name('shop_assignor')->where('id',$param['assignor_id'][$i])->value('name');

            $shop_assignor_records[$i]['single_id'] = Db::name('single')->where('id',$param['single_id'][$i])->value('cat_name');

            $shop_assignor_records[$i]['assignor_price'] = $param['assignor_price'][$i];

            $shop_assignor_records[$i]['assignor_content'] = Db::name('single')->where('id',$param['assignor_content'][$i])->value('cat_name');

            $shop_assignor_records[$i]['create_time'] = date('Y-m-d H:i:s', time());



        }

        return ['code'=>200,'message' =>'ok','data' => $shop_assignor_records];



    }



}